/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.economic;

import gov.fema.hazus.bdc.shared.entity.Economy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class EconomicTextBoxKeyListener implements EconomicTextBoxKeyListenerInterface {

	private Economy economy;
	
	public EconomicTextBoxKeyListener(Economy economy)
	{
		this.economy = economy;
	}
	
	public void onFocus(Widget arg0) {
		// do nothing
	}

	public void onLostFocus(Widget arg0) {

		TextBox textBox = (TextBox) arg0;
		String boxName = textBox.getName();
		
		// get the form data as string, long or int
		String value = textBox.getText();
		
		Double doubleValue = null;
		Integer intValue = null;
		
		// try int
		try {
			intValue = Integer.parseInt(value);
		} catch (NumberFormatException e) {
			// swallow
		}	

		// try double
		try {
			doubleValue = Double.parseDouble(value);
		} catch (NumberFormatException e) {
			// swallow
		}
		
		GWT.log("text box name=" + boxName, null);
		
		if( boxName.equals(Economy.BUILDINGVALUE) )
		{
			GWT.log( Economy.BUILDINGVALUE + ": " + value, null);
			this.economy.setBuildingValue(doubleValue);
		} else if ( boxName.equals( Economy.VALUATIONTYPE ) ) {
			GWT.log( Economy.VALUATIONTYPE + ": " + value, null);
			this.economy.setValuationType(value);			
		} else if ( boxName.equals( Economy.VALUATIONYEAR ) ) {
			GWT.log( Economy.VALUATIONYEAR + ": " + value, null);
			this.economy.setValuationYear(intValue);			
		} else if ( boxName.equals( Economy.CONTENTSVALUE ) ) {
			GWT.log( Economy.CONTENTSVALUE + ": " + value, null);
			this.economy.setContentsValue(doubleValue);
		} else if ( boxName.equals( Economy.INVENTORYVALUE ) ) {
			GWT.log( Economy.INVENTORYVALUE + ": " + value, null);
			this.economy.setInventoryValue(doubleValue);
		} else if ( boxName.equals( Economy.RENTALINCOME ) ) {
			GWT.log( Economy.RENTALINCOME + ": " + value, null);
			this.economy.setRentalIncomePerMonth(doubleValue);
		} else if ( boxName.equals( Economy.BUSINESSINCOME ) ) {
			GWT.log( Economy.BUSINESSINCOME + ": " + value, null);
			this.economy.setBusinessIncomePerDay(doubleValue);		
		} else if ( boxName.equals( Economy.WAGESPERDAY ) ) {
			GWT.log( Economy.WAGESPERDAY + ": " + value, null);
			this.economy.setWagesPerDay(doubleValue);	
		} else {
			// do nothing
		}
	}

	public void updateEconomy(Economy economy) {
		this.economy = economy;
	}
}
